package middleware

import (
	"addsvc"
	"addsvc/internal/utils/method"
	"context"

	"github.com/go-kit/log"
)

type ServiceMiddleware = func(addsvc.Service) addsvc.Service

func LoggingService(logger log.Logger) ServiceMiddleware {
	return func(next addsvc.Service) addsvc.Service {
		return loggingMiddleware{logger, next}
	}
}

type loggingMiddleware struct {
	logger log.Logger
	next   addsvc.Service
}

func (mw loggingMiddleware) Sum(ctx context.Context, a, b int) (int, error) {
	defer func() {
		mw.logger.Log("method", method.Sum, "a", a, "b", b)
	}()
	return mw.next.Sum(ctx, a, b)
}

func (mw loggingMiddleware) Concat(ctx context.Context, a, b string) (string, error) {
	defer func() {
		mw.logger.Log("method", method.Concat, "a", a, "b", b)
	}()
	return mw.next.Concat(ctx, a, b)
}
